# -*- coding:utf8 -*-
# !/usr/bin/python
'''
author: ning.luo
createTime: 2021/11/15 10:08
update: ning.luo
updateTime: 2021/11/15 10:08
description: 单例模式学习
'''
import threading
import time
from queue import Queue


class A:

    _instance_lock = threading.Lock()

    def __init__(self, x):
        self.a = x

    def __new__(cls, *args, **kwargs):
        if not hasattr(A, "_instance"):
            with A._instance_lock:
                A._instance = object.__new__(cls)

        return A._instance

class QueueDemo:

    def __init__(self):
        demo_queue = Queue(10)
        for i in range(10):
            demo_queue.put(i)
        while not demo_queue.empty():
            print(demo_queue.get())


class MultiThreading:

    def __init__(self):
        self.q = Queue(20)

    def producer(self, name):
        for i in range(1, 2001):
            info = name + "的娃娃%s" % str(i)
            self.q.put(info)

    def consumer(self, name):
        while True:
            try:
                info =self.q.get(timeout=3)
            except Exception as e:
                print("超时无法获取队列内容：", str(e))
                break
            print(name + "拿了%s" % info)

    def main_run(self):
        t1 = threading.Thread(target=self.producer, args=("pro",))
        t2 = threading.Thread(target=self.consumer, args=("con",))
        t3 = threading.Thread(target=self.consumer, args=("con1",))
        t1.start()
        t2.start()
        t3.start()
        t1.join()
        t2.join()
        t3.join()
        print("退出主线程")

if __name__ == "__main__":
    aaa = MultiThreading()
    aaa.main_run()